
<%@page import="java.util.List"%>
<%@ page import="com.cn.trip.bean.Cart" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script src="scripts/jquery-3.3.1.js"></script>
<title>购物车</title>
</head>
<style>
.head{
	text-align: center;
	margin-top:20px;
	margin-bottom: 50px;
}
.content{
	margin: 0 auto;
	
}
.table{
text-align: center;
}
.table_center{
	display: inline-block;
}
#bookTable{
	border-right: 1px solid #000;
	border-bottom: 1px solid #000;
	border-collapse:collapse;	
}
#bookTable th{
	width:130px;
	height:30px;
	border-left: 1px solid #000;
	border-top: 1px solid #000;
	
}
#bookTable td{
	border-left: 1px solid #000;
	border-top: 1px solid #000;
}
</style>
<%String path=request.getContextPath(); %>
<body>

<div class="head">
<span>欢迎你${user.username}</span>
<a href="">退出</a>
</div>
<%
		List<Cart> list=(List<Cart>)request.getAttribute("cart");
		float sum=0;
		int num=0;
		for(int i=0;i<list.size();i++){
			sum+=(list.get(i).getGoods_num())*(list.get(i).getGoods().getPrice());
			num+=list.get(i).getGoods_num();
		}
	%>
<div class="content">
	<div class="table">
	<div class="table_center">
	
	你的购物车中有<%=num%>本书
	<table id="bookTable" >
		<tr id="head">
	        <th>序号</th>
	        <th>商品名</th>
	        <th>数量</th>
	        <th>金额</th>
	        <th>操作</th>
	    </tr>
	    <c:forEach var="c" items="${cart}" varStatus="status">
	    <tr>
	    <td>${status.count} </td>
	    <td>${c.goods.goods_name}</td>
	    <td><input type="text" value="${c.goods_num } " maxlength="10" onKeyUp="tttt(this)"/><span id="goods_id" style="display:none;">${c.id}</span></td>
	    <td>${c.goods.price}</td>
	    <td><a href="#" onclick="deleteToCart(${c.id},this)">删除</a></td>
		</tr>
		</c:forEach>
	</table>
	
	总金额：￥<span id="countMoney"><%=sum%></span><br>
	<a href="<%=path%>/GetGoodsList?pageNum=1">继续购物</a>
	<a href="#" onclick="clearCart()">清空购物车</a>
	<a href="<%=path%>/GotoCheckOut" >结账</a>
	</div>
	</div>
	</div>
</body>
<script type="text/javascript">
		function clearCart(){
			$.get("ClearCart",function(data){
				if(data>0){
					$($("#head").nextAll()).remove();
				}
			});
		}
		function tttt(a){
			//alert($(a).next().html());
			var id=$(a).next().html()
			$.get("UpdateCart?id="+id+"&num="+a.value,function(data){
				if(data>0){
					var sum=0;
					$("#bookTable").find("input").each(function(item,index) {
						var a=index.value*($(index).parent().next().html());
						sum=sum+a;
						});

					$("#countMoney").html(sum);
				}
			});
			
		}
		
		function deleteToCart(id,a){
			$.get("DeleteToCart?id="+id,function(data){
				if(data>0){
					$(a).parent().parent().remove();
					$("#countMoney").html(0)
				}
			});
		}

</script>
</html>